﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Sisocana.Core.Interfaces;
using Sisocana.Core;
using Sisocana.Core.Models;
using System.IO;

namespace Sisocana.Services
{
    public class SearchPeople : ISearchPeople
    {
        private IPersonaService _personaService;

        public SearchPeople()
            : this(new PersonaService())
        {
        }

        public SearchPeople(IPersonaService personaService)
        {
            _personaService = personaService;
        }

        public List<Person> AdvancedSearchPerson(SearchParameters sp)
        {
            IList<PersonaDesaparecidaModel> pdm = new List<PersonaDesaparecidaModel>();

            DateTime from = sp.missingDateFrom ?? new DateTime();
            DateTime to = sp.missingDateTo ?? new DateTime();
            pdm = _personaService.SearchPersonAdvancedLocal(sp.siteId, sp.firstName, sp.lastName, sp.phone, from, to);
            List<Person> ret = new List<Person>();
            foreach (var item in pdm)
            {
                Person p = new Person();
                p.id = item.id;
                p.siteId = item.idSitio;
                if (item.nombre != null)
                    p.firstName = item.nombre.Trim();
                else
                    p.firstName = "Unknown";
                if (item.apellido != null)
                    p.lastName = item.apellido.Trim();
                else
                    p.lastName = "Unknown";
                if (item.celular != null)
                    p.phone = item.celular.Trim();
                else
                    p.phone = "Unknown";

                ret.Add(p);
            }
            return ret;
        }

        public List<Person> keywordSearchPerson(int idSitio, string keyword)
        {
            IList<PersonaDesaparecidaModel> pdm = new List<PersonaDesaparecidaModel>();
            pdm = _personaService.SearchPersonByKeywordLocal(keyword);
            List<Person> ret = new List<Person>();
            foreach (var item in pdm)
            {
                Person p = new Person();
                p.id = item.id;
                p.siteId = item.idSitio;
                if (item.nombre != null)
                    p.firstName = item.nombre.Trim();
                else
                    p.firstName = "Unknown";
                if (item.apellido != null)
                    p.lastName = item.apellido.Trim();
                else
                    p.lastName = "Unknown";
                if (item.celular != null)
                    p.phone = item.celular.Trim();
                else
                    p.phone = "Unknown";
                ret.Add(p);
            }
            return ret;
        }

        PersonDetails ISearchPeople.getPersonDetails(int idSitio, int id)
        {
            PersonaDesaparecidaModel p = (PersonaDesaparecidaModel) _personaService.findPersonaDesaparecida(idSitio,id);
            DireccionModel dm = _personaService.findDireccion(idSitio,id);
            IList<ReporteModel> rms = _personaService.findReportes(idSitio, id);
            
            PersonDetails ret = new PersonDetails();
            
            if (p.foto != null)
            {
                ret.photo = new DataImage();
                if (p.foto.imgByte != null)
                {
                    MemoryStream ms = null;
                    MemoryStream ms1 = null;
                    ret.photo.imgByte = p.foto.imgByte;
                    try
                    {
                        ms = new MemoryStream(p.foto.imgByte);
                        System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(ms);
                        ImageResize ir = new ImageResize();
                        ir.File = fullsizeImage;
                        ir.Height = 50;
                        ir.Width = 50;

                        ms1 = new MemoryStream();
                        ir.GetThumbnail().Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);

                        ret.photo.imgByte = ms1.ToArray();
                    }
                    catch (Exception e)
                    {
                        ret.photo.imgByte = null;
                    }
                    finally
                    {
                        ms.Close();
                        ms1.Close();
                    }
                }
                if (p.foto.imgExt != null)
                    ret.photo.imgExt = p.foto.imgExt;
                if (p.foto.imgNombre != null)
                    ret.photo.imgName = p.foto.imgNombre;
            } 
            ret.age = p.edad;
            ret.description = p.descripcion.Trim();
            ret.firstName = p.nombre.Trim();
            ret.lastName = p.apellido.Trim();
            ret.missingDate = p.fecha;
            if (p.celular != null)
                ret.phone = p.celular.Trim();
            else
                ret.phone = p.celular;
            ret.siteId = p.idSitio;
            if(p.sexo!=null)
                ret.genre = p.sexo.Trim();
            ret.id = p.id;
            ret.address = new Address();
            ret.address.address = dm.Dir.Trim();
            ret.address.city = dm.Ciudad.Trim();
            if (dm.CoordX != null)
                ret.address.coordX = dm.CoordX.Trim();
            else
                ret.address.coordX = null;
            if (dm.CoordY != null)
                ret.address.coordY = dm.CoordY.Trim();
            else
                ret.address.coordY = null;
            ret.address.country = dm.Pais.Trim();
            if (dm.CP != null)
                ret.address.cp = dm.CP.Trim();
            else
                ret.address.cp = null;
            ret.address.state = dm.Estado.Trim();
            ret.reports = new List<Report>();
            foreach (ReporteModel rm in rms)
            {
                Report r = new Report();
                if (rm.CoordX != null)
                    r.CoordX = rm.CoordX.Trim();
                else
                    r.CoordX = null;
                if (rm.CoordY != null)
                    r.CoordY = rm.CoordY.Trim();
                else
                    r.CoordY = null;
                r.email = rm.email.Trim();
                r.date = rm.fecha;
                r.firstName = rm.nombre.Trim();
                r.lastName = rm.apellido.Trim();
                r.message = rm.mensaje.Trim();
                r.personId = id;
                if (rm.telefono != null)
                    r.phone = rm.telefono.Trim();
                else
                    r.phone = null;
                r.siteId = idSitio;
                r.status = rm.status.Trim();
                ret.reports.Add(r);
            }

            return ret;
        }

        int ISearchPeople.addReport(Report r)
        {
            ReporteModel rm = new ReporteModel();
            rm.apellido = r.lastName;
            rm.CoordX = r.CoordX;
            rm.CoordY = r.CoordY;
            rm.email = r.email;
            rm.fecha= r.date;
            rm.idPersona = r.personId;
            rm.mensaje = r.message;
            rm.nombre= r.firstName;
            rm.status=r.status;
            rm.telefono=r.phone;
            rm.externalEntity = false;
            int result = -1;
            try{
                result = _personaService.agregarReportePersonaDesaparecida(0,rm);
            }
            catch(Exception e)
            {
                return e.GetHashCode();
            }
            return result;

        }
    }
}
